package com.xiyu.service.repository.medicine;

import com.xiyu.service.vo.health.medicine.MedicinePageQueryInput;
import com.xiyu.service.model.medicine.HealthMedicine;
import com.xiyu.service.model.medicine.HealthMedicineTable;
import org.babyfish.jimmer.spring.repository.JRepository;
import org.babyfish.jimmer.Page;
import org.springframework.util.StringUtils;
import java.util.Optional;

public interface HealthMedicineRepository extends JRepository<HealthMedicine, Long> {
    HealthMedicineTable healthMedicineTable = HealthMedicineTable.$;

    default Page<HealthMedicine> pageQuery(MedicinePageQueryInput inputVO){
        return sql().createQuery(healthMedicineTable)
                        .whereIf(StringUtils.hasText(inputVO.getMedicineName()), () -> healthMedicineTable.medicineName().like(inputVO.getMedicineName()))
                        .whereIf(StringUtils.hasText(inputVO.getMedicineBrevityCode()), () -> healthMedicineTable.medicineBrevityCode().like(inputVO.getMedicineBrevityCode()))
                        .orderBy(healthMedicineTable.id().desc())
                        .select(healthMedicineTable)
                .fetchPage(inputVO.getPageNo() - 1, inputVO.getPageSize());
    }

    Optional<HealthMedicine> findFirstByMedicineName(String medicineName);


}